#pragma once
#include "TcpConnection.h"
#include "Dictionary.h"
#include <queue>
#define TOP_N 10

//保存关键字，关键字的词频，关键字的最小编辑距离的结构体
struct MyResult{
    string _word;
    int _freq;
    int _dist;
    //依次按照最小编辑距离最小、词频最高、字典顺序进行排序
    friend bool operator<(const MyResult &lhs, const MyResult &rhs){
        if(lhs._dist != rhs._dist){
            return lhs._dist > rhs._dist;
        }
        else{
            if(lhs._freq != rhs._freq){
                return lhs._freq < rhs._freq;
            }
            else{
                if(lhs._word != rhs._word){
                    return lhs._word > rhs._word;
                }
                else{
                    return true;
                }
            }
        }
    }
};

//候选词推荐类
class KeyRecommander{
    using TcpConnectionPtr = shared_ptr<TcpConnection>;
public:
    KeyRecommander(string &query, const TcpConnectionPtr &conn, Dictionary *dict);
    ~KeyRecommander();
    void execute(int thread_id);
private:
    pair<set<int>, set<int>> queryIndexTable();
    void statistic(pair<set<int>, set<int>> setPair);
    int distance(const string &rhs);
    void response(int thread_id);
private:
    string &_queryWord;
    const TcpConnectionPtr &_conn;
    Dictionary *_dict;
    priority_queue<MyResult, vector<MyResult>> _resultQue;
};

